package com.prawn.trace.enterprise.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.prawn.trace.enterprise.dao.CompanyDao;
import com.prawn.trace.enterprise.pojo.Company;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class CompanyService {

	@Autowired
	private CompanyDao companyDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<Company> findAll() {
		return companyDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Company> findSearch(Map whereMap, int page, int size) {
		Specification<Company> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return companyDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<Company> findSearch(Map whereMap) {
		Specification<Company> specification = createSpecification(whereMap);
		return companyDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public Company findById(String id) {
		return companyDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param company
	 */
	public void add(Company company) {
		company.setId( idWorker.nextId()+"" );
		companyDao.save(company);
	}

	/**
	 * 修改
	 * @param company
	 */
	public void update(Company company) {
		companyDao.save(company);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		companyDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<Company> createSpecification(Map searchMap) {

		return new Specification<Company>() {

			@Override
			public Predicate toPredicate(Root<Company> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // id
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 企业名称
                if (searchMap.get("companeName")!=null && !"".equals(searchMap.get("companeName"))) {
                	predicateList.add(cb.like(root.get("companeName").as(String.class), "%"+(String)searchMap.get("companeName")+"%"));
                }
                // 地址
                if (searchMap.get("address")!=null && !"".equals(searchMap.get("address"))) {
                	predicateList.add(cb.like(root.get("address").as(String.class), "%"+(String)searchMap.get("address")+"%"));
                }
                // 邮箱
                if (searchMap.get("email")!=null && !"".equals(searchMap.get("email"))) {
                	predicateList.add(cb.like(root.get("email").as(String.class), "%"+(String)searchMap.get("email")+"%"));
                }
                // 电话
                if (searchMap.get("tel")!=null && !"".equals(searchMap.get("tel"))) {
                	predicateList.add(cb.like(root.get("tel").as(String.class), "%"+(String)searchMap.get("tel")+"%"));
                }
                // 网址
                if (searchMap.get("webAddress")!=null && !"".equals(searchMap.get("webAddress"))) {
                	predicateList.add(cb.like(root.get("webAddress").as(String.class), "%"+(String)searchMap.get("webAddress")+"%"));
                }
                // 企业介绍
                if (searchMap.get("introduce")!=null && !"".equals(searchMap.get("introduce"))) {
                	predicateList.add(cb.like(root.get("introduce").as(String.class), "%"+(String)searchMap.get("introduce")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 删除标记（虚拟删除，并不删除数据库表数据，0：正常，1：删除）
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
                // 法人资格证书
                if (searchMap.get("qualificationCertificate")!=null && !"".equals(searchMap.get("qualificationCertificate"))) {
                	predicateList.add(cb.like(root.get("qualificationCertificate").as(String.class), "%"+(String)searchMap.get("qualificationCertificate")+"%"));
                }
                // 营业执照注册码
                if (searchMap.get("blRegiserNo")!=null && !"".equals(searchMap.get("blRegiserNo"))) {
                	predicateList.add(cb.like(root.get("blRegiserNo").as(String.class), "%"+(String)searchMap.get("blRegiserNo")+"%"));
                }
                // 营业所在地
                if (searchMap.get("blAddress")!=null && !"".equals(searchMap.get("blAddress"))) {
                	predicateList.add(cb.like(root.get("blAddress").as(String.class), "%"+(String)searchMap.get("blAddress")+"%"));
                }
                // 营业期限
                if (searchMap.get("blDate")!=null && !"".equals(searchMap.get("blDate"))) {
                	predicateList.add(cb.like(root.get("blDate").as(String.class), "%"+(String)searchMap.get("blDate")+"%"));
                }
                // 营业范围
                if (searchMap.get("blCourse")!=null && !"".equals(searchMap.get("blCourse"))) {
                	predicateList.add(cb.like(root.get("blCourse").as(String.class), "%"+(String)searchMap.get("blCourse")+"%"));
                }
                // 组织机构代码
                if (searchMap.get("organizationalCode")!=null && !"".equals(searchMap.get("organizationalCode"))) {
                	predicateList.add(cb.like(root.get("organizationalCode").as(String.class), "%"+(String)searchMap.get("organizationalCode")+"%"));
                }
                // 企业荣誉，证书
                if (searchMap.get("honors")!=null && !"".equals(searchMap.get("honors"))) {
                	predicateList.add(cb.like(root.get("honors").as(String.class), "%"+(String)searchMap.get("honors")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
